package com.van.utils;

import com.van.entity.account.User;
import com.van.service.account.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * 工具
 * @author VanKa qq:2395073039
 * @date 2021/8/4 18:15
 */
public class ToolsUtils {

    /**
     * 生成userToKen
     * @param id id
     * @param type type
     * @return userToKen
     */
    public static String createUserToKen( Long id, int type){
        return id + ":" + type;
    }

    /**
     * 获取用户Id
     * @param request request
     * @return id
     */
    public static Long getUserID(HttpServletRequest request){
        String user = request.getHeader("userToKen");
        String[] split = user.split(":");
        if (split.length == 2){
            return Long.parseLong(split[0]);
        }
        return 0L;
    }
    /**
     * 获取用户类型
     * @param request request
     * @return type
     */
    public static int getUserType(HttpServletRequest request){
        String user = request.getHeader("userToKen");
        String[] split = user.split(":");
        if (split.length == 2){
            return Integer.getInteger(split[1]);
        }
        return -1;
    }
    /**
     * 获取用户数据
     * @param request request
     * @return type
     */
    public static User getUser(HttpServletRequest request, UserService userService){
        String user = request.getHeader("userToKen");
        String[] split = user.split(":");
        if (split.length == 2){
            return userService.getById(split[0]);
        }
        return null;
    }
}
